package producers;

import java.io.File;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import net.sf.json.JSONObject;
import play.Logger;
import play.libs.WS;
import play.libs.WS.HttpResponse;
import play.libs.WS.WSRequest;
import utils.StringUtil;
import utils.UrlUtil;
import utils.WSUtil;

public class MediaProducer implements IMediaProducer {
    
    public String sid;
    
    public String token;
    
    public String appId;
    
    public MediaProducer(String sid,String token,String appId){
        this.sid = sid;
        this.token = token;
        this.appId = appId;
    }
    
   private final static String BASE_URL="http://www.ucpaas.com/maap/ipcc/voice/uploadFile";

    @Override
    public Map<String, String> upload(String sid, String token, String appId, File file) {
          String result =  WSUtil.uploadFile(BASE_URL, sid, token, appId, file);
          Map<String,String> results = new HashMap<String,String>();
          if(!StringUtil.isBlank(result)){
              JSONObject obj = JSONObject.fromObject(result);
              JSONObject _resp = obj.getJSONObject("resp");
              results.put("rcode", _resp.getString("respCode"));
              results.put("msg", _resp.getString("respMsg"));
              results.put("fileName", file.getName());
              if(_resp.containsKey("file")){
                  results.put("fileId", _resp.getJSONObject("file").getString("fileId"));
              }
          }
          if(file.exists()){
              file.delete();
          }
         Logger.info("[%s]: sid<%s>, token<%s>, appId<%s>, file<%s>, results<%s>", "upload",sid,token,appId,file,results);
        return results;
    }

}
